package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置类，用来生成RedisTemplate对象，自定义key或value的序列化器
 */
@Slf4j
@Configuration
public class RedisConfiguration {
    
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
        log.info("生成RedisTemplate对象...");
        //创建redisTemplate
        RedisTemplate redisTemplate= new RedisTemplate();
        //设置key的序列化器，指定用字符串序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //设置value的序列化器，指定用字符串序列化器【不推荐设置value的序列化器】
        // redisTemplate.setValueSerializer(new StringRedisSerializer());

        //通过redis的连接工厂创建redisTemplate对象
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
    
}
